import { _decorator, Component, Node, input, Input, EventKeyboard, KeyCode, Vec2, RigidBody, Vec3, Collider, ICollisionEvent, ITriggerEvent } from 'cc';
import { Food } from './Food';
const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Component {
    @property
    public speed = 5;
    @property
    public moveForce = 5;
    private moveDir:Vec2 = Vec2.ZERO;
    private moveDir3:Vec3 = Vec3.ZERO;
    private rgd = null;
    
    protected start(): void {
        this.rgd = this.getComponent(RigidBody);
        let cld = this.node.getComponent(Collider);
        //cld.on('onCollisionEnter', this.onColliderEnter, this);
        cld.on('onTriggerEnter', this.onTriggerEnter, this);
    }

    protected onLoad(): void {
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_PRESSING, this.onKeyPress, this);
    }
    protected onDestroy(): void {
        input.off(Input.EventType.KEY_UP, this.onKeyDown, this);
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.off(Input.EventType.KEY_PRESSING, this.onKeyPress, this);
    }

    onTriggerEnter(event: ITriggerEvent){
        console.log('onTriggerEnter');
        const food = event.otherCollider.getComponent(Food);
        if(food !== null){
            food.node.destroy();
        }
    }
/*     onColliderEnter(event: ICollisionEvent){
        console.log('onColliderEnter');
        const food = event.otherCollider.getComponent(Food);
        if(food !== null){
            food.node.destroy();
        }

    } */

    onKeyDown(event: EventKeyboard): void {
        switch(event.keyCode){
            case KeyCode.KEY_A:
                this.moveDir = new Vec2(-1, this.moveDir.y);
                this.moveDir3 = new Vec3(this.moveDir3.x, this.moveDir3.y, -1);
                break;
            case KeyCode.KEY_D:
                this.moveDir = new Vec2(1, this.moveDir.y);
                this.moveDir3 = new Vec3(this.moveDir3.x, this.moveDir3.y, 1);
                break;
            case KeyCode.KEY_W:
                this.moveDir = new Vec2(this.moveDir.x, 1);
                this.moveDir3 = new Vec3(1, this.moveDir3.y, this.moveDir3.z);
                break;
            case KeyCode.KEY_S:
                this.moveDir = new Vec2(this.moveDir.x, -1);
                this.moveDir3 = new Vec3(-1, this.moveDir3.y, this.moveDir3.z);
                break;
        }
    }
    onKeyUp(event: EventKeyboard): void {
        switch(event.keyCode){
            case KeyCode.KEY_A:
                this.moveDir = new Vec2(0, this.moveDir.y);
                this.moveDir3 = new Vec3(this.moveDir3.x, this.moveDir3.y, 0);
                break;
            case KeyCode.KEY_D:
                this.moveDir = new Vec2(0, this.moveDir.y);
                this.moveDir3 = new Vec3(this.moveDir3.x, this.moveDir3.y, 0);
                break;
            case KeyCode.KEY_W:
                this.moveDir = new Vec2(this.moveDir.x, 0);
                this.moveDir3 = new Vec3(0, this.moveDir3.y, this.moveDir3.z);
                break;
            case KeyCode.KEY_S:
                this.moveDir = new Vec2(this.moveDir.x, 0);
                this.moveDir3 = new Vec3(0, this.moveDir3.y, this.moveDir3.z);
                break;
        }
    }
    onKeyPress(event: EventKeyboard): void {
        /* console.log('onKeyPress:  ', event.keyCode);
        switch(event.keyCode){
            case KeyCode.KEY_A: // A
                this.node.setPosition(this.node.position.x, 
                    this.node.position.y, this.node.position.z-0.2);
                break;
            case KeyCode.KEY_D: // D
                this.node.setPosition(this.node.position.x, 
                    this.node.position.y, this.node.position.z+0.2);
                break;
            case KeyCode.KEY_W: // W
                this.node.setPosition(this.node.position.x+0.2, 
                    this.node.position.y, this.node.position.z);
                break;
            case KeyCode.KEY_S: // S
                this.node.setPosition(this.node.position.x-0.2, 
                    this.node.position.y, this.node.position.z);
                break;
        } */
    }
    protected update(dt: number): void {
        //this.rgd.applyForce(new Vec3(this.moveDir.y, 0, this.moveDir.x).multiplyScalar(this.moveForce));
        this.rgd.applyForce(new Vec3(this.moveDir3.x, this.moveDir3.y, this.moveDir3.z).multiplyScalar(this.moveForce));
    }

}


